/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.figures;


import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.LabelFigure;

/**
 * Property Figure.
 * 
 * @author Patrick Tessier
 */
public class PropertyFigure extends LabelFigure {
	//constant
	/**
	 * 
	 */
	protected static String _public=" +";
	
	/**
	 * 
	 */
	protected static String _protected=" #";
	
	/**
	 * 
	 */
	protected static String _private=" -";
	
	/**
	 * 
	 */
	protected static String _package="~";
	
	/**
	 * 
	 */
	protected static String _derived="/";
	
	/**
	 * 
	 */
	protected static String _ordered="ordered";
	
	/**
	 * 
	 */
	protected static String _readonly="readOnly";
	
	/**
	 * 
	 */
	protected static String _subsets="subsets";
	
	/**
	 * 
	 */
	protected static String _redefines="redefine";
	
	/**
	 * 
	 */
	protected static String _unique="unique";
	
	/**
	 * 
	 */
	protected static String _union="union";


	/**
	 * 
	 */
	protected Label label=new Label();
	
	/**
	 * 
	 */
	protected String visibility=_public;
	
	/**
	 * 
	 */
	protected String name="";
	
	/**
	 * 
	 */
	protected String type="<undefined>";
	
	/**
	 * 
	 */
	protected String derived="";
	
	/**
	 * 
	 */
	protected String defaultValue="";
	
	/**
	 * 
	 */
	protected String multiplicity="";
	
	/**
	 * 
	 */
	protected String propModifier="";
	
	/**
	 * 
	 */
	protected boolean isStatic=false;
	
	/**
	 * 
	 */
	protected String stereotypes="";


	/**
	 * 
	 */
	public PropertyFigure()
	{
		FontData[] font={new FontData("Arial", 9, SWT.NORMAL)};
		setFont(PapyrusPlugin.fontManager.get(font));
		setLabelAlignment(PositionConstants.LEFT );
	}

	/**
	 * update property label.
	 * 
	 * @param newText 
	 */
	public void display(String newText) {
		setText(stereotypes+newText);
	}

	/**
	 * to add stereotypes.
	 * 
	 * @param name of the operation
	 * @param st 
	 */
	@Override
	public void setStereotypes(String st){
		if (st==null) {
			this.stereotypes="";
		} else {
			this.stereotypes=PapyrusPlugin.ST_LEFT+st+PapyrusPlugin.ST_RIGHT;
		}
		display();
	}

	/**
	 * display the name of the property.
	 * 
	 * @param name of the property
	 */
	@Override
	public void setName(String name){
		this.name=name;
		setText(visibility+" "+name);
		display();
	}

	/**
	 * display the type of the property.
	 * 
	 * @param type of the property
	 */
	public void setType(String type){
		this.type=": "+type;
		display();
	}
	
	/**
	 * Display default value.
	 * 
	 * @param the default value
	 * @param value 
	 */
	public void setDefaultValue(String value){
		if (value!=null)
		{this.defaultValue="= "+value;} else {
			value="";
		}

		display();
	}

	/**
	 * to indicate if the propety is derived.
	 * 
	 * @param b , true the property is derived, false other
	 */
	public void setDerived(boolean b){
		if (b) {
			derived=" "+_derived;
		} else {
			derived="";
		}
		display();
	}
	
	/**
	 * to indicate information about property as unique...
	 * 
	 * @param isUnion 
	 * @param ordered 
	 * @param unique 
	 * @param ordered, true, if ordered
	 * @param isUnion, true if union
	 * @param unique, true if unique
	 * @param readOnly 
	 * @param readOnly, true if readonly
	 */
	public void setProperty(boolean unique,boolean readOnly,boolean ordered,boolean isUnion){
		if (!unique &&!readOnly &&!ordered &&!isUnion) {
			propModifier="";
		} else{
			propModifier=" {";
			if (readOnly) {
				propModifier=propModifier+_readonly+", ";
			}
			if	(isUnion) {
				propModifier=propModifier+_union+", ";
			}
			if (ordered) {
				propModifier=propModifier+_ordered+", ";
			}
			if (unique) {
				propModifier=propModifier+_unique+", ";
			}
			propModifier=propModifier.substring(0, propModifier.length()-2)+"}";
			display();
		}
	}


	/**
	 * diplay the property.
	 */
	protected void display(){
		setText(visibility+derived+" "+name+type+multiplicity+ defaultValue+propModifier);
	}

	/**
	 * diplay the property.
	 * 
	 * @return 
	 */
	public String getDisplay(){
		return visibility+derived+" "+name+type+multiplicity+ defaultValue+propModifier;
	}
	
	/**
	 * display the multiplicity.
	 * 
	 * @param upper integer
	 * @param lower integer
	 */
	public void setMultiplicity(int lower, int upper){
		if((lower==1) && (upper==1)){
			multiplicity="";
		}
		else{
			multiplicity="["+lower+"..";
			if (upper==-1)
			{multiplicity=multiplicity+"*]";}
			else{
				multiplicity=multiplicity+upper+"]";
			}

		}
		display();
	}
	
	/**
	 * display as private.
	 */
	public void setPrivate(){
		this.visibility=_private;
		display();
	}
	
	/**
	 * display as protected.
	 */
	public void setProtected(){
		this.visibility=_protected;
		display();
	}
	
	/**
	 * display as public.
	 */
	public void setPublic(){
		this.visibility=_public;
		display();
	}
	
	/**
	 * display as package.
	 */
	public void setPackage(){
		this.visibility=_package;
		display();
	}

	/**
	 * display the operation as abstract.
	 * 
	 * @param b true for abstract; false other
	 */
	public void setAbstract(boolean b){
		if (b){
			FontData[] font={new FontData("Arial", 9, SWT.ITALIC)};
			this.setFont(PapyrusPlugin.fontManager.get(font));
		}
		else{
			FontData[] font={new FontData("Arial", 9, SWT.NORMAL)};
			this.setFont(PapyrusPlugin.fontManager.get(font));
		}
	}
	
	/**
	 * display the element as static.
	 * 
	 * @param b 
	 */
	public void setStatic(boolean b){
		this.isStatic=b;
	}
	
	/**
	 * paint the label.
	 * 
	 * @param graphics 
	 */
	@Override
	public void paint (Graphics graphics) {
		super.paint (graphics);
		Rectangle rect;
		rect = this.getBounds();
		if (isStatic){
			graphics.setBackgroundColor(ColorConstants.black);
			Dimension labelSize=calculateTextSize();
			graphics.drawLine(rect.x+2, rect.y+rect.height-2, rect.x+labelSize.width, rect.y+rect.height-2); //separator between compartement
		}
	}
}
